﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using Telerik.WebControls;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class BillReturnVisit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                link.OnClientClick = JsExtension.CreateModalWindowScript("../LookUp/NoticeSend.aspx?messageDesc=" + Evaluationdesc.Text.Trim().ToString(), noticeID.ClientID, 600, 500);
                BindInstrumentList(1);
                WsmMaintainEvaluationEntity evaEntity = new WsmMaintainEvaluationEntity();
                if (Request.QueryString["EvaluationID"] != null)
                {
                    evaEntity = new SystemBll().Get<WsmMaintainEvaluationEntity>(new Guid(Request.QueryString["EvaluationID"]));
                    IsTrack.Checked = evaEntity.IsTrack ==0 ? false:true;
                    Evaluationdesc.Text = evaEntity.Evaluationdesc;
                }
            }
        }
        private void BindInstrumentList(int pageindex)
        {           
            List<SqlDbParameter> parms = new List<SqlDbParameter>();            
            int RecordCount = 0;
            List<WsmEvaluationEntity> list = new SystemBll().GetAll<WsmEvaluationEntity>(parms, "CreatedOn desc", AspNetPager1.PageSize, pageindex, out RecordCount);
            RepeaterInstruments.DataSource = list;
            RepeaterInstruments.DataBind();
            AspNetPager1.RecordCount = RecordCount;                     
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindInstrumentList(AspNetPager1.CurrentPageIndex);
        }

        protected void RepeaterInstruments_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label ItemID = e.Item.FindControl("ItemID") as Label;
            RadNumericTextBox itemValue = e.Item.FindControl("itemValue") as RadNumericTextBox;

            if (ItemID != null) {
                if (Request.QueryString["EvaluationID"] != null) {
                    List<WsmMaintainEvaluationItemEntity> list = new SystemBll().GetAll<WsmMaintainEvaluationItemEntity>(" and EvaluationID='" + Request.QueryString["EvaluationID"] + "' and itemID='" + ItemID.Text + "'");
                    if (list != null && list.Count > 0) {
                        itemValue.Value = System.Convert.ToDouble(list[0].itemValue);
                    }
                }
            }
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            string Message = "";
            if (!save(ref Message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "saveandssscloseerror", "alert('" + Message + "');", true);
            }
            else
                ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "window.close();", true);
        }

        private bool save(ref string message)
        {
            if (IsTrack.Checked) {
                if (noticeID.Value.Trim() == "") {
                    message = "请发送跟踪消息";
                    return false;
                }
            }
            WsmMaintainEvaluationEntity evaEntity = new WsmMaintainEvaluationEntity();
            if (Request.QueryString["EvaluationID"] != null) {
                evaEntity = new SystemBll().Get<WsmMaintainEvaluationEntity>(new Guid(Request.QueryString["EvaluationID"]));             
                new SystemBll().Delete<WsmMaintainEvaluationItemEntity>(" and EvaluationID = '" + Request.QueryString["EvaluationID"] + "'");
            }
            if (Request.QueryString["MaintainID"] != null) {
                evaEntity.MaintainID = new Guid(Request.QueryString["MaintainID"]);
                evaEntity.EvaluationID = Guid.NewGuid();
                evaEntity.Deleted = false;
                evaEntity.CreatedOn = DateTime.Now;
                evaEntity.CreatedBy = CurrentUserID;
            }
            evaEntity.Evaluationdesc = Evaluationdesc.Text.Trim();
            evaEntity.IsTrack = IsTrack.Checked ? 1 : 0;
            new SystemBll().InsertOrUpdate(evaEntity);
            for (int index = 0; index < RepeaterInstruments.Items.Count;index++)
            {
                Label ItemID = RepeaterInstruments.Items[index].FindControl("ItemID") as Label;
                RadNumericTextBox itemValue = RepeaterInstruments.Items[index].FindControl("itemValue") as RadNumericTextBox;
                if (itemValue.Value != null) {
                    WsmMaintainEvaluationItemEntity itemEntity = new WsmMaintainEvaluationItemEntity();
                    itemEntity.EvaluationItemID = Guid.NewGuid();
                    itemEntity.EvaluationID = evaEntity.EvaluationID;
                    itemEntity.itemID = new Guid(ItemID.Text);
                    itemEntity.itemValue = System.Convert.ToDecimal(itemValue.Value);
                    new SystemBll().Insert(itemEntity);
                }
            }
            return true;
        }

        protected void link_Click(object sender, EventArgs e)
        {            
        }

        protected void Evaluationdesc_TextChanged(object sender, EventArgs e)
        {
            link.OnClientClick = JsExtension.CreateModalWindowScript("../LookUp/NoticeSend.aspx?messageDesc=" + HttpUtility.UrlEncode(Evaluationdesc.Text.Trim().ToString()), noticeID.ClientID, 600, 500);
        }
    }
}
